package com.saasitems.admin.web.controller;

import com.saasitems.admin.model.Original;
import com.saasitems.admin.service.OriginalService;
import com.saasitems.admin.web.vo.matts.MattsVO;
import com.saasitems.admin.web.vo.original.OriginalQueryVO;
import com.saasitems.admin.web.vo.original.OriginalVO;
import com.saasitems.common.model.PageInfo;
import com.saasitems.common.utils.CheckUtil;
import com.saasitems.common.utils.ConstantUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/12/15.
 */
@Controller
public class OriginalController extends BaseController {

    @Autowired
    private OriginalService originalService;

    @RequestMapping(value="index",method= RequestMethod.GET)
    public ModelAndView i() {
        return new ModelAndView("admin/original/index");
    }

    @RequestMapping(value="admin/original/index/{classId}",method= RequestMethod.GET)
    public ModelAndView index(@PathVariable("classId") String classId, HttpServletRequest request) {
        HttpSession session = request.getSession();
        // 将外面传入的classId保存在session中
        session.setAttribute(ConstantUtil.CLASSID_IN_SESSION, classId);

        OriginalQueryVO queryVO = new OriginalQueryVO();
        queryVO.setClassId(classId);

        PageInfo page = new PageInfo(1, 10);
        List<Original> list = originalService.query(queryVO, page);
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("info", list);
        model.put("queryvo", new OriginalQueryVO());
        model.put("page", page);
        return new ModelAndView("admin/original/index", model);
    }

    @RequestMapping(value="admin/original/index",method= RequestMethod.GET)
    public ModelAndView index(HttpServletRequest request) {
        HttpSession session = request.getSession();
        String classId = (String) session.getAttribute(ConstantUtil.CLASSID_IN_SESSION);

        OriginalQueryVO queryVO = new OriginalQueryVO();
        queryVO.setClassId(classId);

        Map<String, Object> model = new HashMap<String, Object>();
        if (StringUtils.isNotBlank(classId)) {
            PageInfo page = new PageInfo(1, 10);
            List<Original> list = originalService.query(queryVO, page);
            model.put("info", list);
            model.put("queryvo", new OriginalQueryVO());
            model.put("page", page);
        }
        return new ModelAndView("admin/original/index", model);
    }

    @RequestMapping(value="admin/original/index",method= RequestMethod.POST)
    public ModelAndView index(OriginalQueryVO queryVO, PageInfo page, HttpServletRequest request) {
        HttpSession session = request.getSession();
        // 将外面传入的classId保存在session中
        String classId = (String) session.getAttribute(ConstantUtil.CLASSID_IN_SESSION);
        queryVO.setClassId(classId);

        List<Original> list = originalService.query(queryVO, page);
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("info", list);
        model.put("queryvo", queryVO);
        model.put("page", page);
        return new ModelAndView("admin/original/index", model);
    }

    @RequestMapping(value="admin/original/add",method= RequestMethod.GET)
    public ModelAndView add() {
        Map<String, Object> model = new HashMap<String, Object>();
        return new ModelAndView("admin/original/add", model);
    }

    @RequestMapping(value="admin/original/add",method= RequestMethod.POST)
    @ResponseBody
    public Object add(OriginalVO vo, HttpServletRequest request) {
        Map<String, Object> model = new HashMap<String, Object>();

        try {
            String ret = CheckUtil.checkContent(vo.getContent());
            if (StringUtils.isNotBlank(ret)) {
                return this.renderError(ret);
            }

            HttpSession session = request.getSession();
            // 将外面传入的classId保存在session中
            String classId = (String) session.getAttribute(ConstantUtil.CLASSID_IN_SESSION);
            vo.setClassId(classId);
            originalService.save(vo);
            return this.renderSuccess("添加成功", this.convertUrl(request, "admin/original/index/" + classId + ".shtml"));
        } catch (Exception e) {
            return this.renderError("添加失败");
        }
    }

    @RequestMapping(value="admin/original/edit/{id}",method= RequestMethod.GET)
    public ModelAndView edit(@PathVariable("id") Integer id) {
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("id", id);

        OriginalVO vo = this.originalService.getInfoById(id);
        model.put("info", vo);
        return new ModelAndView("admin/original/edit", model);
    }

    @RequestMapping(value="admin/original/edit/{id}",method= RequestMethod.POST)
    @ResponseBody
    public Object edit(OriginalVO vo, @PathVariable("id") Integer id, HttpServletRequest request) {
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("id", id);
        vo.setId(id);

        try {
            this.originalService.update(vo);
            return this.renderSuccess("更新成功", this.convertUrl(request, "admin/original/index.shtml"));
        } catch (Exception e) {
            return this.renderError("更新失败");
        }
    }


    @RequestMapping(value="admin/original/delete/{id}",method= RequestMethod.GET)
    @ResponseBody
    public Object delete(@PathVariable("id") Integer id, HttpServletRequest request) {
        Map<String, Object> model = new HashMap<String, Object>();
        OriginalVO vo = new OriginalVO();
        model.put("id", id);
        vo.setId(id);

        try {
            this.originalService.delete(vo);
            return this.renderSuccess("删除成功", this.convertUrl(request, "admin/original/index.shtml"));
        } catch (Exception e) {
            return this.renderError("删除失败");
        }
    }
}
